<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：文章控制器。
 *
 **/

namespace Qwadmin\Controller;

use Vendor\Tree;

class SellvoucherController extends ComController{

    public function add(){

        //凭证号
        $res = array();
        $res['voucher_no'] = date("YmdHis",time());
        $this->assign("res",$res);


        //粮食品种
        $Foodstuff = M("foodstuff");
        $foodstuff_array = $Foodstuff->order("o asc")->select();
        $this->assign("foodstuff_array",$foodstuff_array);

        //仓库
        $Warehouse = M("warehouse");
        $warehouse_array = $Warehouse->select();
        $this->assign("warehouse_array",$warehouse_array);

//        //车型
//        $Cartype = M("cartype");
//        $cartype_array = $Cartype->select();
//        $this->assign("cartype_array",$cartype_array);


        //时间
        $date = time();
        $this->assign("date",$date);

        $this->display("form");
    }

    public function index($sid = 0, $p = 1)
    {

        $Sellvoucher = M("sellvoucher");
        $pagesize = 20;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');

//        $where = "1=1";
        if(I("keyword")!=''){
            $where['voucher_no'] = array("like","%".I("keyword")."%");
        }

        $count = $Sellvoucher->where($where)->count();
        $list = $Sellvoucher
            ->where($where)
            ->limit($offset . ',' . $pagesize)
            ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }

    public function del(){
        $id = I("get.id");
        if ($id) {
            $map['id'] = $id;
            if (M('sellvoucher')->where($map)->delete()) {
                addlog('删除凭证，ID：' . $id);
                $this->success('凭证删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }
    public function edit(){


        //粮食品种
        $Foodstuff = M("foodstuff");
        $foodstuff_array = $Foodstuff->order("o asc")->select();
        $this->assign("foodstuff_array",$foodstuff_array);

        //仓库
        $Warehouse = M("warehouse");
        $warehouse_array = $Warehouse->select();
        $this->assign("warehouse_array",$warehouse_array);



        $Sellvoucher = M("sellvoucher");
        $id = I("id");
        $res = $Sellvoucher->find($id);
        $this->assign("res",$res);

//        $Parameter = M("parameter");
//        $Parameter_setting = M("parameter_setting");
//        $map['foodstuff_id'] = $res['foodstuff_id'];
//        $map['level'] = $res['level'];
//        $parameter_res = $Parameter->where($map)->find();
//        $setting_res = $Parameter_setting->where("parameter_id=".$parameter_res['id'])->find();

//        $this->assign("parameter_res",$parameter_res);
//        if(!empty($setting_res)){
//            $shuifen_res = unserialize($setting_res['shuifen']);
//            $shengmei_res = unserialize($setting_res['shengmei']);
//            $zazhi_res = unserialize($setting_res['zazhi']);
//            $chucaolv_res = unserialize($setting_res['chucaolv']);
//            $zhengjingmilv_res = unserialize($setting_res['zhengjingmilv']);
//            $guwaicaomilv_res = unserialize($setting_res['guwaicaomilv']);
//            $huanglimi_res = unserialize($setting_res['huanglimi']);
//
//            $this->assign("isParameter","1");
//            $this->assign("shuifen_res",$shuifen_res);
//            $this->assign("shengmei_res",$shengmei_res);
//            $this->assign("zazhi_res",$zazhi_res);
//            $this->assign("chucaolv_res",$chucaolv_res);
//            $this->assign("zhengjingmilv_res",$zhengjingmilv_res);
//            $this->assign("guwaicaomilv_res",$guwaicaomilv_res);
//            $this->assign("huanglimi_res",$huanglimi_res);
//        }
        $this->display("form");
    }

    public function update(){

        $Sellvoucher = D("Sellvoucher");
        if(!$Sellvoucher->create()){
            $this->error($Sellvoucher->getError());
        }
        $data = I("post.");
        if(isset($data['id']) && $data['id']!=""){
            $res = $Sellvoucher->data($data)->save();
        if($res!==false){
                $this->success("销售凭证已编辑成功",U("index"));
            }else{
                $this->error("销售凭证编辑失败!");
            }
        }else{
            $data['voucher_no'] = date("YmdHis",time());
            $data['date'] = time();
//            dump($data);exit;

            $id = $Sellvoucher->data($data)->add();
            if($id){
                $this->success("销售凭证已成功记录",U("index"));
            }else{
                $this->error("销售凭证记录失败!");
            }
        }

    }


    function getPrice(){
        $where['foodstuff_id'] = I("foodstuff_id");
        $where['level'] = I("level");
        $Parameter = M("parameter");
        $parameter_res = $Parameter->where($where)->find();

        if(!empty($parameter_res)){

            $json['status'] = 1;
            $json['price'] = $parameter_res['price'];

        }else{
            $json['status'] = 2;//2为没有参数数据
        }

        echo json_encode($json);

    }





}